package com.ant.business.system.service.impl;

import com.ant.business.system.entity.Unit;
import com.ant.business.system.mapper.UnitDao;
import com.ant.business.system.service.UnitService;
import com.ant.common.constant.SystemConstant;
import com.ant.common.exceptions.BusinessException;
import com.ant.common.utils.FastMap;
import com.ant.common.utils.IdWorker;
import com.ant.common.utils.LocalBeanUtil;
import com.ant.common.utils.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 单位管理Service实现类
 *
 * @author
 * @description
 * @create 2021-03-22 14:25:56
 */
@Service("unitService")
@Transactional(rollbackFor = Throwable.class)
public class UnitServiceImpl extends ServiceImpl<UnitDao, Unit> implements UnitService {

    private final UnitDao unitDao;

    public UnitServiceImpl(UnitDao unitDao) {
        this.unitDao = unitDao;
    }

    /**
     * 分页查询列表
     *
     * @param page
     * @param limit
     * @param param
     * @return
     */
    @Override
    public PageInfo<Unit> getList(int page, int limit, FastMap param) {
        QueryWrapper<Unit> wrapper = new QueryWrapper<>();

        wrapper.ne("delete_flag", 1);

        if (StringUtil.isNotEmpty(param.getString("unitName"))) {
            wrapper.like("unit_name", param.getString("unitName"));
        }

        if (StringUtil.isNotEmpty(param.getString("linkman"))) {
            wrapper.like("linkman", param.getString("linkman"));
        }

        if (param.getStatus("status") != -1) {
            wrapper.eq("status", param.getStatus("status") - 1);
        }

        PageHelper.startPage(page, limit);
        List<Unit> list = unitDao.selectList(wrapper);
        return new PageInfo<>(list);
    }

    /**
     * 新增信息
     *
     * @param unit
     */
    @Override
    @SneakyThrows
    public void saveOne(Unit unit) {
        unit.setStatus(SystemConstant.UNIT_STATUS.ACCEPTED.getCode());
        unit.setDeleteFlag(SystemConstant.DELETE_STATUS.UN_DELETE.getCode());
        LocalBeanUtil.setLoginUserInfo(unit);
        int count = unitDao.insert(unit);
        if (count != 1) {
            throw new BusinessException("单位保存失败");
        }
    }

    /**
     * 更新信息
     *
     * @param unit
     */
    @Override
    @SneakyThrows
    public void update(Unit unit) {
        LocalBeanUtil.updateLoginUserInfo(unit);
        int count = unitDao.updateById(unit);
        if (count != 1) {
            throw new BusinessException("更新单位失败");
        }
    }

    /**
     * 删除单位
     *
     * @param primaryKey
     */
    @Override
    @SneakyThrows
    public void delete(String primaryKey) {
        Unit unit = unitDao.selectById(primaryKey);

        Unit newUnit = new Unit();
        newUnit.setUnitId(Integer.valueOf(primaryKey));
        newUnit.setDeleteFlag(SystemConstant.DELETE_STATUS.DELETED.getCode());
        newUnit.setUnitName(unit.getUnitName() + "_" + IdWorker.get32UUID());
        LocalBeanUtil.updateLoginUserInfo(newUnit);
        int count = unitDao.updateById(newUnit);
        if (count != 1) {
            throw new BusinessException("删除单位失败");
        }
    }
}
